/**
  ******************************************************************************
  * @file    _tc32l010_flash.h
  * @author  CHIPAT Application Team
  * @brief   This file contains all the functions prototypes for the FLASH
  *          firmware library.
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __XS32L010_FLASH_H
#define __XS32L010_FLASH_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "tc32l010.h"
#include "tc32l010_rcc.h"

/* FLASH Status */
typedef enum
{
    FLASH_COMPLETE = 1,
    FLASH_READY,
    FLASH_WRPERR,
    FLASH_RDPERR,
    FLASH_BUSY,
    FLASH_TIMEOUT,
} FLASH_Status;

/** @defgroup FLASH_Mode
  * @{
  */
#define FLASH_OP_READ                   ((uint32_t)0x00000000)
#define FLASH_OP_PROGRAM                ((uint32_t)0x00000001)
#define FLASH_OP_SECTORERASE            ((uint32_t)0x00000002)
#define FLASH_OP_CHIPERASE              ((uint32_t)0x00000003)

#define IS_FLASH_OP(MODE)         (((MODE) == FLASH_OP_READ)     || \
                                   ((MODE) == FLASH_OP_PROGRAM)  || \
                                   ((MODE) == FLASH_OP_SECTORERASE) || \
                                   ((MODE) == FLASH_OP_CHIPERASE))

/** @defgroup Flash_Latency 
  * @{
  */ 
#define FLASH_Latency_0                ((uint32_t)0x00000000)    /*!< FLASH Zero Latency cycle      */
#define FLASH_Latency_1                FLASH_TIME_LATENCY        /*!< FLASH One Latency cycle       */

#define IS_FLASH_LATENCY(LATENCY) (((LATENCY) == FLASH_Latency_0)  || \
                                   ((LATENCY) == FLASH_Latency_1))


#define FLASH_LVCTL_0                ((uint32_t)0x00000000)     /*!< FLASH Working voltage     */
#define FLASH_LVCTL_1                FLASH_TIME_LVCTL           /*!< FLASH Working voltage       */

#define IS_FLASH_LVCTL(LVCTL)     (((LVCTL) == FLASH_LVCTL_0)  || \
                                   ((LVCTL) == FLASH_LVCTL_1))


/** @defgroup FLASH_Interrupts
  * @{
  */
#define FLASH_IT_EOP                   ((uint32_t)0x00000004)   /*!< End of FLASH Operation Interrupt source */
#define FLASH_IT_ERR                   ((uint32_t)0x00000008)   /*!< Error Interrupt source */
#define IS_FLASH_IT(IT) ((((IT) & (uint32_t)0xFFFFFFF3) == 0x00000000) && ((IT) != 0x00000000))
/**
  * @}
  */

/** @defgroup FLASH_Flags FLASH Flags
  * @{
  */
#define FLASH_FLAG_BSY                 (FLASH_CR_BUSY)       /* FLASH Busy flag */
#define FLASH_FLAG_EOP                 (FLASH_IFR_EOP)       /* FLASH End of Operation flag */
#define FLASH_FLAG_WRPERR              (FLASH_IFR_WRPERR)    /* FLASH Write protected error flag */
#define FLASH_FLAG_RDPERR              (FLASH_IFR_RDPERR)    /* FLASH Option error flag */


#define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFFEFFF8) == 0x00000000) && ((FLAG) != 0x00000000))

#define IS_FLASH_GET_FLAG(FLAG)  (((FLAG) == FLASH_FLAG_BSY) || ((FLAG) == FLASH_FLAG_EOP) || \
                                  ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_RDPERR))




/** @defgroup FLASH_Sectors
  * @{
  */
/**
  * @}
  */

#define OB_WRProt_Sector0_3         ((uint32_t)0xFFFFFFFE) /* Write protection of sector 0   3  */
#define OB_WRProt_Sector4_7         ((uint32_t)0xFFFFFFFD) /* Write protection of sector 4   7  */
#define OB_WRProt_Sector8_11        ((uint32_t)0xFFFFFFFB) /* Write protection of sector 8   11 */
#define OB_WRProt_Sector12_15       ((uint32_t)0xFFFFFFF7) /* Write protection of sector 12  15 */
#define OB_WRProt_Sector16_19       ((uint32_t)0xFFFFFFEF) /* Write protection of sector 16  19 */
#define OB_WRProt_Sector20_23       ((uint32_t)0xFFFFFFDF) /* Write protection of sector 20  23 */
#define OB_WRProt_Sector24_27       ((uint32_t)0xFFFFFFBF) /* Write protection of sector 24  27 */
#define OB_WRProt_Sector28_31       ((uint32_t)0xFFFFFF7F) /* Write protection of sector 28  31 */
#define OB_WRProt_Sector32_35       ((uint32_t)0xFFFFFEFF) /* Write protection of sector 32  35 */
#define OB_WRProt_Sector36_39       ((uint32_t)0xFFFFFDFF) /* Write protection of sector 36  39 */
#define OB_WRProt_Sector40_43       ((uint32_t)0xFFFFFBFF) /* Write protection of sector 40  43 */
#define OB_WRProt_Sector44_47       ((uint32_t)0xFFFFF7FF) /* Write protection of sector 44  47 */
#define OB_WRProt_Sector48_51       ((uint32_t)0xFFFFEFFF) /* Write protection of sector 48  51 */
#define OB_WRProt_Sector52_55       ((uint32_t)0xFFFFDFFF) /* Write protection of sector 52  55 */
#define OB_WRProt_Sector56_59       ((uint32_t)0xFFFFBFFF) /* Write protection of sector 56  59 */
#define OB_WRProt_Sector60_63       ((uint32_t)0xFFFF7FFF) /* Write protection of sector 60  63 */
#define OB_WRProt_Sector64_67       ((uint32_t)0xFFFEFFFF) /* Write protection of sector 64  67 */
#define OB_WRProt_Sector68_71       ((uint32_t)0xFFFDFFFF) /* Write protection of sector 68  71 */
#define OB_WRProt_Sector72_75       ((uint32_t)0xFFFBFFFF) /* Write protection of sector 72  75 */
#define OB_WRProt_Sector76_79       ((uint32_t)0xFFF7FFFF) /* Write protection of sector 76  79 */
#define OB_WRProt_Sector80_83       ((uint32_t)0xFFEFFFFF) /* Write protection of sector 80  83 */
#define OB_WRProt_Sector84_87       ((uint32_t)0xFFDFFFFF) /* Write protection of sector 84  87 */
#define OB_WRProt_Sector88_91       ((uint32_t)0xFFBFFFFF) /* Write protection of sector 88  91 */
#define OB_WRProt_Sector92_95       ((uint32_t)0xFF7FFFFF) /* Write protection of sector 92  95 */
#define OB_WRProt_Sector96_99       ((uint32_t)0xFEFFFFFF) /* Write protection of sector 96  99 */
#define OB_WRProt_Sector100_103     ((uint32_t)0xFDFFFFFF) /* Write protection of sector 100 103*/
#define OB_WRProt_Sector104_107     ((uint32_t)0xFBFFFFFF) /* Write protection of sector 104 107*/
#define OB_WRProt_Sector108_111     ((uint32_t)0xF7FFFFFF) /* Write protection of sector 108 111*/
#define OB_WRProt_Sector112_115     ((uint32_t)0xEFFFFFFF) /* Write protection of sector 112 115*/
#define OB_WRProt_Sector116_119     ((uint32_t)0xDFFFFFFF) /* Write protection of sector 116 119*/
#define OB_WRProt_Sector120_123     ((uint32_t)0xBFFFFFFF) /* Write protection of sector 120 123*/
#define OB_WRProt_Sector124_127     ((uint32_t)0x7FFFFFFF) /* Write protection of sector 124 127*/
#define OB_WRProt_AllSectors        ((uint32_t)0x00000000) /* Write protection of all sectors   */

#define IS_OB_WRP(PAGE)             (((PAGE) == 0xFFFFFFFF)           || ((PAGE) == OB_WRProt_AllSectors)   || \
                                     ((PAGE) == OB_WRProt_Sector0_3)  || ((PAGE) == OB_WRProt_Sector4_7)    ||\
                                     ((PAGE) == OB_WRProt_Sector8_11) || ((PAGE) == OB_WRProt_Sector12_15)  ||\
                                     ((PAGE) == OB_WRProt_Sector16_19)|| ((PAGE) == OB_WRProt_Sector20_23)  ||\
                                     ((PAGE) == OB_WRProt_Sector24_27)|| ((PAGE) == OB_WRProt_Sector28_31)  ||\
                                     ((PAGE) == OB_WRProt_Sector32_35)|| ((PAGE) == OB_WRProt_Sector36_39)  ||\
                                     ((PAGE) == OB_WRProt_Sector40_43)|| ((PAGE) == OB_WRProt_Sector44_47)  ||\
                                     ((PAGE) == OB_WRProt_Sector48_51)|| ((PAGE) == OB_WRProt_Sector52_55)  ||\
                                     ((PAGE) == OB_WRProt_Sector56_59)|| ((PAGE) == OB_WRProt_Sector60_63)  ||\
                                     ((PAGE) == OB_WRProt_Sector64_67)|| ((PAGE) == OB_WRProt_Sector68_71)  ||\
                                     ((PAGE) == OB_WRProt_Sector72_75)|| ((PAGE) == OB_WRProt_Sector76_79)  ||\
                                     ((PAGE) == OB_WRProt_Sector80_83)|| ((PAGE) == OB_WRProt_Sector84_87)  ||\
                                     ((PAGE) == OB_WRProt_Sector88_91)|| ((PAGE) == OB_WRProt_Sector92_95)  ||\
                                     ((PAGE) == OB_WRProt_Sector96_99)|| ((PAGE) == OB_WRProt_Sector100_103)||\
                                     ((PAGE) == OB_WRProt_Sector104_107)|| ((PAGE) == OB_WRProt_Sector108_111)||\
                                     ((PAGE) == OB_WRProt_Sector112_115)|| ((PAGE) == OB_WRProt_Sector116_119)||\
                                     ((PAGE) == OB_WRProt_Sector120_123)|| ((PAGE) == OB_WRProt_Sector124_127))


#define IS_FLASH_ADDRESS(ADDRESS)   (((ADDRESS) >= FLASH_BASE) && ((ADDRESS) <= FLASH_END))
#define IS_OB_DATA_ADDRESS(ADDRESS) ((((ADDRESS) >= OB_BASE) && ((ADDRESS) < (OB_BASE + 0x14))) ||\
                                     (((ADDRESS) >= 0x1FFFFC00U) && ((ADDRESS) < (0x1FFFFC14U))))

#define IS_FLASH_CLK(CLK) (((CLK) == 4000000UL) || ((CLK) == 24000000UL))


/* Exported functions --------------------------------------------------------*/
void FLASH_SetLatency(uint32_t FLASH_Latency, uint32_t FLASH_Lvctl);
void FLASH_SetTimming(uint32_t FLASH_CLK);
void FLASH_Unlock(void);
void FLASH_Lock(void);
void FLASH_EnterDeepPowerDownMode(void);
void FLASH_ExitDeepPowerDownMode(void);
FlagStatus FLASH_GetFlagStatus(uint16_t FLASH_FLAG);
void FLASH_ClearFlag(uint16_t FLASH_FLAG);
FLASH_Status FLASH_GetStatus(void);
void FLASH_ITConfig(uint16_t FLASH_IT, FunctionalState NewState);
FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);
FLASH_Status FLASH_EraseSector(uint32_t FLASH_Sector);
FLASH_Status FLASH_EraseChip(void);
FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data);
FLASH_Status FLASH_ProgramPage(uint32_t Address, uint32_t Data[]);
FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP);
FLASH_Status FLASH_OB_RDPConfig(FunctionalState NewState);
FLASH_Status FLASH_OB_UserConfig(uint8_t user);
FLASH_Status FLASH_OB_BOOTConfig(uint8_t OB_BOOT);
FLASH_Status FLASH_OB_SWDConfig(FunctionalState NewState);
uint8_t FLASH_OB_GetUser(void);
uint32_t FLASH_OB_GetWRP(void);
FlagStatus FLASH_OB_GetRDP(void);
uint8_t FLASH_OB_GetFlashSzie(void);
void FLASH_OB_ResetCmd(FunctionalState NewState);





/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /* __XS32L010_FLASH_H */

/************************ (C) COPYRIGHT CHIPAT *****END OF FILE****/
